﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.Utils;
using HitachiLib.Data;
using HitachiLib.HitachiServiceRef;
using HitachiApp.Model.Resources;

namespace HitachiApp.Model.Manage
{
    public class ResourceBrowserModel : INotifyPropertyChanged
    {
        public ResourceBrowserModel(ManageViewModel parent)
        {
            manage_view_model = parent;
        }

        private ManageViewModel manage_view_model;
        private ObservableCollection<ResourceManageItem> resource_list = new ObservableCollection<ResourceManageItem>();
        ResourceManageItem current_selected;

        public ObservableCollection<ResourceManageItem> RootResources
        {
            get { return resource_list; }
        }
        public ResourceManageItem CurrentSelected
        {
            get { return current_selected; }
            set {
                current_selected = value;
                manage_view_model.RefreshCurrent();
            }
        }

        public void Load(Collection<ResourceData> list)
        {
            List<ResourceManageItem> all = new List<ResourceManageItem>();
            foreach (ResourceData rd in list)
            {
                Resource r = Resource.GetUnique(rd);
                ResourceManageItem rmi = new ResourceManageItem();
                rmi.Data = r;
                all.Add(rmi);
            }
            LinkResources(all);
        }

        private void LinkResources(List<ResourceManageItem> project_items)
        {
            Dictionary<int, ResourceManageItem> m = new Dictionary<int, ResourceManageItem>();
            Dictionary<int, ResourceManageItem> added = new Dictionary<int, ResourceManageItem>();
            resource_list.Clear();
            foreach (ResourceManageItem pmi in project_items)
            {
                m.Add(pmi.Data.Id, pmi);
            }
            foreach (ResourceManageItem pmi in project_items)
            {
                AddResourceWithParent(m, added, pmi);
            }
        }

        private void AddResourceWithParent(Dictionary<int, ResourceManageItem> all, Dictionary<int, ResourceManageItem> added, ResourceManageItem pmi)
        {
            if (added.ContainsKey(pmi.Data.Id))
                return;
            if (!all.ContainsKey(pmi.Data.Data.ParentID))
            {
                resource_list.Add(pmi);
            }
            else
            {
                ResourceManageItem parent = all[pmi.Data.Data.ParentID];
                pmi.Parent = parent;
                if (!parent.Children.Contains(pmi))
                    parent.Children.Add(pmi);
                AddResourceWithParent(all, added, parent);
            }
            added.Add(pmi.Data.Id, pmi);
        }

        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
